package com.ghh.provider.controller;

import com.ghh.provider.domain.Goods;
import com.ghh.provider.service.GoodsService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Value("${server.port}")
    private int port;

    @GetMapping("/findOne/{id}")
    @HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties = {
            //设置Hystrix的超时时间，默认1s
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000"),
    })
    public Goods findOne(@PathVariable("id") int id){

        if (id == 1){
            int i = 1/0 ;
        }

        Goods goods = goodsService.findOne(id);

        goods.setTitle(goods.getTitle() + ":" + port);//将端口号，设置到了 商品标题上
        return goods;
    }

    /**
     * 定义降级方法：
     *  1. 方法的返回值需要和调用方法一样
     *  2. 方法的参数（个数和类型）需要和调用方法一样
     */
    public Goods findOne_fallback(int id){

        Goods goods = new Goods();

        goods.setTitle("系统维护中~~~");

        return goods;
    }
}
